﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SIA.HwAccess;

namespace SIA.AutomationControl
{
    public enum LedStatus
    {
        Undefined = 0,
        Off = 1,
        On = 2
    }

    public enum LedColors
    {
        Grigio = 0,
        Rosso = 1,
        Giallo = 2,
        Verde = 3,
        Blu = 4
    }

    public partial class Led : UserControl
    {

        private LedColors _ledColor=LedColors.Grigio;
        private LedStatus _ledStatus=LedStatus.Off;
        private IDigitalSignal _signal = null;
        private Object _activeValue = true;

        public Led()
        {
            InitializeComponent();
        }

        public LedStatus Status
        {
            get { return this._ledStatus; }
            set
            {
                _ledStatus = value;
                if (_ledStatus == LedStatus.On)
                    BackgroundImage = imageList.Images[(int)_ledColor];
                if (_ledStatus == LedStatus.Off)
                    BackgroundImage = imageList.Images[(int)LedColors.Grigio];
            }
        }

        public LedColors Color
        {
            get { return _ledColor; }
            set {   
                _ledColor = value;
                if (_ledStatus == LedStatus.On) BackgroundImage = imageList.Images[(int)_ledColor];
            }
        }

        public IHwSignal Signal
        {
            get { return (IHwSignal)_signal; }
            set
            {
                if (value is IDigitalSignal)
                {
                    _signal = (IDigitalSignal)value;
                    this.Status = _signal.DigitalValue.Equals(_activeValue) ? LedStatus.On : LedStatus.Off;
                    _signal.ValueChange += new SignalEvent(_signal_ValueChangeEvent);

                }
                else
                {
                    if (_signal != null) _signal.ValueChange -= new SignalEvent(_signal_ValueChangeEvent);
                    _signal = null;
                }
            }
        }


        public Object ActiveValue { get { return _activeValue; } set {
            if (_activeValue != value)
            {
                _activeValue = value;
                if (_signal != null)
                    this.Status = _signal.DigitalValue.Equals(_activeValue) ? LedStatus.On : LedStatus.Off;
            }
        } }

        void _signal_ValueChangeEvent(IHwSignal signal)
        {
            this.Invoke(new MethodInvoker(delegate
            {
                this.Status = ((IDigitalSignal)signal).DigitalValue ? LedStatus.On : LedStatus.Off;
            }));
        }

        #region Trasparenza

        //protected override void OnPaintBackground(PaintEventArgs pevent)
        //{
            //non permette il repainting dello sfondo
        //}

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                return cp;
            }
        }

        #endregion
    }

}